﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows;

namespace FieldDevice.UI
{
    public partial class DCDeviceForm : DeviceFormBase
    {
        public DCDeviceForm(Device device)
            :base(device)
        {
            InitializeComponent();
            base.ReadChangeEventHander = new Device.ReadChangeEventHander(device_ReadChangeEvent);
            base.UpdateReadsDelegate = new VoidDelegate(this.UpdateReads);
            this.Yaao_Timer.Enabled = false;
            this.TempDevice = device;
        }

        public  Device TempDevice;
        public DCDeviceForm() 
        {
        }


        void device_ReadChangeEvent(Device device, string name, float value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(base.UpdateReadsDelegate);
            }
            else
                this.UpdateReads();
        }

        public override void UpdateReads()
        {
            ListDisplay.Items.Clear();
            ListOrderCount = 1;
            VoltageGroup1.Text = this.TempDevice.CurrentReads["1#"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "1#", "1#电池电压", TempDevice.CurrentReads["1#"].ToString()+"V"));
            VoltageGroup2.Text = this.TempDevice.CurrentReads["2#"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "2#", "2#电池电压", TempDevice.CurrentReads["2#"].ToString()+"V"));
            VoltageGroup3.Text = this.TempDevice.CurrentReads["3#"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "3#", "3#电池电压", TempDevice.CurrentReads["3#"].ToString()+"V"));
            VoltageGroup4.Text = this.TempDevice.CurrentReads["4#"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "4#", "4#电池电压", TempDevice.CurrentReads["4#"].ToString()+"V"));
            VoltageGroup5.Text = this.TempDevice.CurrentReads["5#"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "5#", "5#电池电压", TempDevice.CurrentReads["5#"].ToString()+"V"));
            VoltageGroup6.Text = this.TempDevice.CurrentReads["6#"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "6#", "6#电池电压", TempDevice.CurrentReads["6#"].ToString()+"V"));
            VoltageGroup7.Text = this.TempDevice.CurrentReads["7#"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "7#", "7#电池电压", TempDevice.CurrentReads["7#"].ToString()+"V"));
            VoltageGroup8.Text = this.TempDevice.CurrentReads["8#"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "8#", "8#电池电压", TempDevice.CurrentReads["8#"].ToString()+"V"));
            VoltageGroup9.Text = this.TempDevice.CurrentReads["9#"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "9#", "9#电池电压", TempDevice.CurrentReads["9#"].ToString()+"V"));
            Vol_AB.Text = this.TempDevice.CurrentReads["ZLP_UAB"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "ZLP_UAB", "AB线电压", TempDevice.CurrentReads["ZLP_UAB"].ToString()+"V"));
            Vol_BC.Text = this.TempDevice.CurrentReads["ZLP_UBC"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "ZLP_UBC", "BC线电压", TempDevice.CurrentReads["ZLP_UBC"].ToString()+"V"));
            Vol_CA.Text = this.TempDevice.CurrentReads["ZLP_UCA"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "ZLP_UCA", "CA线电压", TempDevice.CurrentReads["ZLP_UCA"].ToString()+"V"));
            Vol_HM.Text = this.TempDevice.CurrentReads["ZLP_U0"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "ZLP_U0", "合母电压", TempDevice.CurrentReads["ZLP_U0"].ToString()+"V"));
            Vol_KM.Text = this.TempDevice.CurrentReads["ZLP_UKM"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "ZLP_UKM", "控母电压", TempDevice.CurrentReads["ZLP_UKM"].ToString()+"V"));
            Vol_bat.Text = this.TempDevice.CurrentReads["ZLP_Ubat"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "ZLP_Ubt", "充电电压", TempDevice.CurrentReads["ZLP_Ubat"].ToString()+"V"));
            Cur_CD.Text = this.TempDevice.CurrentReads["ZLP_UCD"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "ZLP_UCD", "充电电流", TempDevice.CurrentReads["ZLP_UCD"].ToString()+"A"));
            Cur_KM.Text = this.TempDevice.CurrentReads["ZLP_IKM"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "ZLP_IKM", "控母电流", TempDevice.CurrentReads["ZLP_IKM"].ToString()+"A"));
            Cur_FD.Text = this.TempDevice.CurrentReads["ZLP_IFD"].ToString("F2");
            ListDisplay.Items.Add(ListDetail(ListOrderCount, "ZLP_IFD", "放电电流", TempDevice.CurrentReads["ZLP_IFD"].ToString()+"A"));
            Analy_Alarm();
        }

        public void Analy_Alarm()
        {
            if (this.Device1.CurrentReads["ZLP_Alaram_1"] != 0)
                this.alarm_AB_Hig.Alarm = true;     
            else
                this.alarm_AB_Hig.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_2"] != 0)
                this.alarm_AB_Low.Alarm = true;
            else
                this.alarm_AB_Low.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_3"] != 0)
                this.alarm_BC_Hig.Alarm = true;
            else
                this.alarm_BC_Hig.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_4"] != 0)
                this.alarm_BC_Low.Alarm = true;
            else
                this.alarm_BC_Low.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_5"] != 0)
                this.alarm_CA_Hig.Alarm = true;
            else
                this.alarm_CA_Hig.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_6"] != 0)
                this.alarm_CA_Low.Alarm = true;
            else
                this.alarm_CA_Low.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_7"] != 0)
                this.alarm_KM_Hig.Alarm = true;
            else
                this.alarm_KM_Hig.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_8"] != 0)
                this.alarm_KM_Low.Alarm = true;
            else
                this.alarm_KM_Low.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_9"] != 0)
                this.alarm_IKM_Hig.Alarm = true;
            else
                this.alarm_IKM_Hig.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_10"] != 0)
                this.alarm_Bat_Hig.Alarm = true;
            else
                this.alarm_Bat_Hig.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_11"] != 0)
                this.alarm_bat_Low.Alarm = true;
            else
                this.alarm_bat_Low.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_12"] != 0)
                this.alarm_HM_Hig.Alarm = true;
            else
                this.alarm_HM_Hig.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_13"] != 0)
                this.alarm_HM_Low.Alarm = true;
            else
                this.alarm_HM_Low.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_14"] != 0)
                this.alarm_ICharge_Hig.Alarm = true;
            else
                this.alarm_ICharge_Hig.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_16"] != 0)
                this.alarmLight1.Alarm = true;
            else
                this.alarmLight1.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_17"] != 0)
                this.alarmLight2.Alarm = true;
            else
                this.alarmLight2.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_18"] != 0)
                this.alarmLight3.Alarm= true;
            else
                this.alarmLight3.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_19"] != 0)
                this.alarmLight4.Alarm = true;
            else
                this.alarmLight4.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_20"] != 0)
                this.alarmLight5.Alarm = true;
            else
                this.alarmLight5.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_21"] != 0)
                this.alarmLight6 .Alarm= true;
            else
                this.alarmLight6.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_22"] != 0)
                this.alarmLight7.Alarm = true;
            else
                this.alarmLight7.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_23"] != 0)
                this.alarmLight8.Alarm = true;
            else
                this.alarmLight8.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_24"] != 0)
                this.alarmLight9.Alarm = true;
            else
                this.alarmLight9.Alarm = false;

            if (this.Device1.CurrentReads["ZLP_Alaram_25"] != 0)
                this.alarm_Iso_Falut.Alarm = true;
            else
                this.alarm_Iso_Falut.Alarm = false;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            this.TempDevice.DebugReads();
            this.UpdateReads();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.TempDevice.DebugReads();
            this.UpdateReads();
        }

        private ListViewItem AlarmFun(int order, string property, string type, string content)
        {
            ListViewItem list = new ListViewItem(order.ToString());
            list.SubItems.Add(property);
            list.SubItems.Add(type);
            list.SubItems.Add(content);
            return list;
        }

        private void DCDeviceForm_Load(object sender, EventArgs e)
        {
            if (this.Device1 != null)
            {
                this.Device1.ReadChangeEvent += this.ReadChangeEventHander;
                this.TempDevice = this.Device1;
            }
            else
                this.Text = "No Device is set!";
        }

    }
}
